package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.FocusUser;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoApiImpl implements VideoApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    /**
     * 保存数据
     *
     * @param video
     * @return
     */
    @Override
    public String save(Video video) {
        //设置vid，vid自动增长
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        mongoTemplate.save(video);
        //保存成功，会自动赋值id
        return video.getId().toHexString();
    }

    /**
     * 根据vid查询数据列表
     * @param vids
     * @return
     */
    @Override
    public List<Video> findVideoByPids(List<Long> vids) {
        Query query = Query.query(Criteria.where("vid").in(vids));
        return mongoTemplate.find(query,Video.class);
    }

    /**
     * 倒序查询
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public List<Video> queryVideoList(int page, Integer pagesize) {
        Query query = new Query().limit(pagesize).skip((page-1)*pagesize).with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query,Video.class);
    }

    /**
     * 视频点赞
     * @param id：视频的ID
     */
    @Override
    public void videoLike(String id) {
        Query query = Query.query(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("likeCount",1);
        mongoTemplate.findAndModify(query,update,Video.class);
    }

    /**
     * 视频取消点赞
     * @param id：视频的ID
     */
    @Override
    public void videoDislike(String id) {
        Query query = Query.query(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("likeCount",-1);
        mongoTemplate.findAndModify(query,update,Video.class);
    }

    /**
     * 关注用户
     * @param focusUser
     * @return
     */
    @Override
    public void focus(FocusUser focusUser) {
        boolean exists = mongoTemplate.exists(Query.query(Criteria.where("userId").is(focusUser.getUserId())
                .and("followUserId").is(focusUser.getFollowUserId())), FocusUser.class);
        if (!exists){
            mongoTemplate.save(focusUser);
        }
    }

    @Override
    public void Unfocus(Long userId, Long uid) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("followUserId").is(uid));
        mongoTemplate.remove(query,FocusUser.class);
    }

    /**
     * 根据视频id查询该条数据
     * @param id
     * @return
     */
    @Override
    public Video queryById(String id) {
        return mongoTemplate.findOne(Query.query(Criteria.where("_id").is(id)),Video.class);
    }

    /**
     * 通过用户id查询全部视频
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult findByUserId(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, Video.class);
        query.skip((page-1)*pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Video> list = mongoTemplate.find(query, Video.class);
        return new PageResult(page,pagesize,count,list);
    }
}
